url="https://apiv3.shanbay.com/team/forum/gbeml/threads"
work_path=$(dirname $(readlink -f $0))
logPath="/var/www/html/shanbay/log-post.txt"
itjsonPath="$work_path/../output/it.json"
reqjsonPath="$work_path/../output/req.json"

activityName=$1
if test -z "$activityName";
then
    activityName="早起团"
fi

key=""
if [ $activityName = "早起团" ]
then
	key="zaoqi"
elif [ $activityName = "惜时社" ]
then
	key="xishi"
elif [ $activityName = "沁心运动" ]
then
	key="yundong"
else
	key="test"
fi

tokenPath=$work_path/../tokens/$key

# remove old it.json & req.json
if [ -f "$itjsonPath" ]
then
	rm $itjsonPath
fi

if [ -f "$reqjsonPath" ]
then
	rm $reqjsonPath
fi

sleep 1s
# get make activity thread contents
/root/.pyenv/shims/python $work_path/run_makeThreadContent.py $1 >> $logPath

sleep 1s
if [ ! -f "$reqjsonPath" ]; then
	echo "$reqjsonPath not exist"
	exit 0
fi

# if [ ! -f "$itjsonPath" ]; then
# 	echo "$itjsonPath not exist"
# 	exit 0
# else
# 	echo "Success to get file $itjsonPath"
# fi

if [ ! -f "$tokenPath" ]; then
	echo "$tokenPath not exist, please save a valid token into it"
	exit 0
else
	token=`cat $tokenPath`
fi

# echo itjson: $itjson
# echo token: $token

# post activity thread
curl -k -X POST -H "Content-Type: application/json" --cookie "auth_token=${token}" -d @$reqjsonPath $url >> $logPath
# echo "@$reqjsonPath $url $logPath" >> $logPath
# sleep 2s
# post candy thread
# curl -k -X POST -H "Content-Type: application/json" --cookie "auth_token=${token}" -d @$itjsonPath $url >> $logPath
